<template>
    <div class="edit-password">

        <el-form ref="form" :model="user" label-width="120px">
            <el-row>
                <el-col :span="12">
                    <el-form-item>
                        <template slot="label"><span style="color: red">*</span>原密码</template>
                        <el-input v-model="user.originalPassword" maxlength="64" style="width:500px"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item>
                        <template slot="label"><span style="color: red">*</span>新密码</template>
                        <el-input v-model="user.newPassword1" maxlength="64" style="width:500px" show-password></el-input>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item>
                        <template slot="label"><span style="color: red">*</span>确认新密码</template>
                        <el-input v-model="user.newPassword2" maxlength="64" style="width:500px" show-password></el-input>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-form-item>
                    <el-button @click="editDataBoxClose">取 消</el-button>
                    <el-button type="primary" @click="onEditSubData">保 存 数 据</el-button>
                </el-form-item>
            </el-row>

        </el-form>


    </div>
</template>

<script>
    import userApi from '@/api/user/userApi'
    import paramApi from '@/api/param/paramApi'
    import  "@/js/desUtils"
    export default {
        name: "EditPassword",
        data(){
            return{
                user:{
                    id:this.$store.getters.getCmsUser.id,
                    originalPassword:'',
                    newPassword1:'',
                    newPassword2:''

                }
            }
        },
        methods:{
            editDataBoxClose(){
                this.$emit('editPasswordDataBoxClose',false)
            },
            async onEditSubData(){
                let user =this.user
                if (user.originalPassword.trim()==''){
                    this.$message.error("原密码不能为空")
                }else if (user.newPassword1.trim()=='' || user.newPassword2.trim() == ''){
                    this.$message.error("新密码不能为空")
                }else if(user.newPassword1.trim()!=user.newPassword2.trim()){
                    this.$message.error("新密码不一致，请重新输入")
                }else {
                    //获取加密的key
                   let res = await paramApi.getByKey("loginSecretKey")
                   let paramValue = res.data.paramValue
                   if (res.code == '200'){
                        //加密密码
                       let originalPassword = DesUtils.strEnc(user.originalPassword,paramValue);
                       let newPassword = DesUtils.strEnc(user.newPassword1,paramValue);
                       let res2 =await userApi.saveUserPassword(user.id,originalPassword,newPassword)
                       if (res2.code == '200'){
                           this.$emit('onUpdatePasswordSuccess',res2.data)
                       }else {
                           this.$message.error(res2.msg)
                       }
                   } else {
                        this.$message.error(res.msg)
                   }
                }

            }

        }
    }
</script>

<style scoped>

</style>